
/**
 * Created by liujie on 2017/10/16.
 */
var express = require('express');
var request = require('request');
var Mock = require('mockjs');
var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var auth = require('../../auth/auth');
var util = require('../../util/commonutil');
var router = express.Router();
var resultJSON = require('../common/resultJSON');


router.get("/",function (req, res, next) {
    res.render("systemManage/powerSetting.ejs",{parentMenuList:false,sysMap:false,serverurl:pro.serveraddr})
});
//获取停车数据
router.post('/queryPowerList',function (req,res,next) {
    var sid=auth.getSessionID(req, res, next);
    var qs=req.body;
    qs.sid = sid;
    //if(qs.isSearch){
        request({
            url:pro.devServeraddr + "heb/busTotalPower/list",
            method: "POST",
            qs:qs
        },function (error, response, body) {
            console.log("电池列表>>"+body);
            var tableResult = {
                total:0,
                rows:[],
                isSuccess:true,//必须是boolean类型
                message:""
            };
            if(!error && response.statusCode == 200){
                var result = JSON.parse(body);
                if(result.success == true){
                    tableResult.isSuccess = true;
                    tableResult.total = result.total;
                    tableResult.rows = result.rows;
                }
            }else{
                tableResult.isSuccess = false;
                tableResult.message = "电池列表查询数据异常，请稍后重试！";
            }
            res.json(tableResult);
        });
    // }else{
    //     var tableResult = {
    //         total:0,
    //         rows:[],
    //         isSuccess:true,//必须是boolean类型
    //         message:""
    //     };
    //     res.json(tableResult);
    // }

});
router.post("/saveData",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/busTotalPower/save",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("新增用户数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="保存数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
router.post("/editData",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/busTotalPower/edit",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("新增用户数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="保存数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
router.post("/deleteUser",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/busTotalPower/delete",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("快速增加设备数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="删除数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="删除数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});

module.exports = router;